"""
Copyright 2007, Thomas Dejanovic.

This is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.

Hatch node factory module. This is useful for automatic hatch-code generation 
as the factory converts strings to HatchNode class instances.
"""

id = "$Id: hatch_node_factory.py 667 2010-07-01 00:12:17Z jayshurtz $"
# $URL: http://hatch.googlecode.com/svn/tags/taggle_release_2.3/hatch/hatch_nodes/hatch_node_factory.py $
# $Author: jayshurtz $
version = " version ".join(id.split()[1:3])


# Import all hatch node modules.
import to_hatch_structure
from hatch_node import *
from hatch_node_physical import *
from hatch_register import *
from hatch_struct import *
# XXX import additional modules here.


class HatchNodeFactory(object):
    """ A Factory for hatch node classes.
    """
    def create(self, nodeClass, name, *childNodes, **properties):
        """ Return an object in the hatch node class tree.
            'nodeClass' is a string equal the desired object's '.__class__.__name__' attribute.
            'name' is the hatch node init function's required 'name' argument.
            The optional 'properties' & 'childNodes' arguments are passed to the object's init function for convenience.
        """
        newNode = None
        nodeClass = nodeClass.strip()
        if nodeClass == HatchNode.__name__:
            newNode = HatchNode(name, *childNodes, **properties)
        elif nodeClass == HatchNodePhysical.__name__:
            newNode = HatchNodePhysical(name, *childNodes, **properties)
        elif nodeClass == Register.__name__:
            newNode = Register(name, *childNodes, **properties)
        elif nodeClass == Struct.__name__:
            newNode = Struct(name, *childNodes, **properties)
        elif nodeClass == Pack.__name__:
            newNode = Pack(name, *childNodes, **properties)
        elif nodeClass == Mux.__name__:
            newNode = Mux(name, *childNodes, **properties)
        # XXX Add additional classes here.
        else:
            pass    # Return None instead of raising error.
        return newNode


# Main for testing.
if __name__ == "__main__":

    # Test class
    hnf = HatchNodeFactory()

    hn = hnf.create('HatchNode ', 'bosco')
    hnp = hnf.create('HatchNodePhysical', 'the')
    r = hnf.create('Register', 'clown', bit_width=6)
    p = hnf.create('Pack ', '', r)
    m = hnf.create('Mux ', 'my', p)
    s = hnf.create('Struct', 'ruined', m)
    n = hnf.create('Moolah', 'party')
    #e = hnf.create('Moolaho')

    print hn.info()
    print hnp.info()
    print r.info()
    print s.info()
    print m.info()
    #print n.info()
    #print e.info()


